package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DEmptyStorageExcution;
import com.fortune.dto.in.dEmptyStorage.DEmptyStorageDto;
import com.fortune.dto.in.dStoreOut.DStoreOutDetailDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DEmptyStorageEnum;
import com.fortune.service.DEmptyStorageService;
import com.fortune.vo.dEmptyStorage.DEmptyDetail;
import com.fortune.vo.dEmptyStorage.DEmptyStorageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by Administrator on 2018/6/20 0020.
 */
@Api(value = "emptyStorage", description = "经销商空箱入库", tags = {"经销商空箱入库"})
@RestController
@RequestMapping("emptyStorage")
public class DEmptyStorageController {
    @Autowired
    private DEmptyStorageService dEmptyStorageService;

    @ApiOperation(value = "经销商空箱入库列表", nickname = "dEmptyStorageList", notes = "", tags={ "@安童" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="search", value="搜索",paramType="query",dataType="string"),
            @ApiImplicitParam(name="driverId", value="司机",paramType="query",dataType="string"),
            @ApiImplicitParam(name="storageStatus", value="入库状态（0全部1已入库2未入库）",paramType="query",dataType="int",allowableValues = "0,1,2"),
            @ApiImplicitParam(name="startDate", value="开始日期",paramType="query",dataType="Date"),
            @ApiImplicitParam(name="endDate", value="结束日期",paramType="query",dataType="Date"),
            @ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
            @ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<DEmptyStorageList>> list(HttpServletRequest request, @ApiIgnore @Validated DEmptyStorageDto dEmptyStorageDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        dEmptyStorageDto.setPage((dEmptyStorageDto.getPage()-1)*10);
        List<DEmptyStorageList> list = dEmptyStorageService.findEmptyStorageList(dEmptyStorageDto,did);
        List<DEmptyStorageList> listSize = dEmptyStorageService.findEmptyStorageLists(dEmptyStorageDto,did);
        int count = listSize.size();
        PagingData<DEmptyStorageList> pagingData = new PagingData<DEmptyStorageList>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DEmptyStorageExcution<PagingData<DEmptyStorageList>>(pagingData, DEmptyStorageEnum.SUCCESS);
    }

    @ApiOperation(value = "空箱入库对应详情列表", nickname = "listDetail", notes = "", tags={ "@安童" })
    @PostMapping(value = "listDetail",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<List<DEmptyDetail>> listDetail(HttpServletRequest request, @Validated @RequestBody DStoreOutDetailDto dStoreOutDetailDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String did = dUserInfo.getdId();
        List<DEmptyDetail> list = dEmptyStorageService.findListDetail(dStoreOutDetailDto,did);
        return new DEmptyStorageExcution<List<DEmptyDetail>>(list, DEmptyStorageEnum.SUCCESS);
    }

    @ApiOperation(value = "确认回空箱", nickname = "dConfirmBox", notes = "", tags={ "@安童" })
    @PutMapping(value = "dConfirmBox",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> confirm(HttpServletRequest request,@Validated @RequestBody DStoreOutDetailDto dStoreOutDetailDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        int result = dEmptyStorageService.updateDConfirmBox(dUserInfo, dStoreOutDetailDto);
        if (result==1) {
            return new DEmptyStorageExcution<Void>(null, DEmptyStorageEnum.SUCCESS);
        }else if (result == -2){
            return new DEmptyStorageExcution<Void>(null, DEmptyStorageEnum.SFALSE);
        }
        return new DEmptyStorageExcution<Void>(null, DEmptyStorageEnum.SUCCESS);
    }

}
